<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card v-loading="loading" class="tree-card">
        <!-- 用了一个行列布局 -->
        <TreeTools :node-data="company" :is-root="false" @showDialog="showDialog" />
        <el-tree :data="departs" :props="defaultProps" default-expand-all>
          <template #default="{data}">
            <TreeTools
              :node-data="data"
              @del-depts="getDepartments"
              @showDialog="showDialog"
              @edit-depts="editDepts"
            />
          </template>
        </el-tree>
      </el-card>
    </div>
    <!-- 因为添加和编辑是共用这一个弹窗的，所以需要封装组件，方便复用 -->
    <AddDepts
      ref="deptRef"
      :dialog-visible.sync="dialogVisible"
      :depts="depts"
      :node-data="nodeData"
      @add-depts="getDepartments"
    />
  </div>
</template>

<script>
import AddDepts from './component/add-depts.vue'
import TreeTools from './component/tree-tools.vue'
import { reqGetDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
export default {
  components: {
    TreeTools,
    AddDepts
  },
  data() {
    return {
      loading: false,
      dialogVisible: false,
      company: {},
      departs: [],
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      depts: [],
      nodeData: {}
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    async getDepartments() {
      this.loading = true
      const { data: { data: { depts, companyName }}} = await reqGetDepartments()
      // console.log(JSON.parse(JSON.stringify(depts)))
      this.company = {
        name: companyName,
        manager: '负责人'
      }
      this.departs = tranListToTreeData(depts, '')
      this.depts = depts
      this.loading = false
    },
    closeDialog(flag) {
      this.dialogVisible = flag
    },
    showDialog(nodeData) {
      this.dialogVisible = true
      this.nodeData = nodeData
    },
    async editDepts(nodeData) {
      this.dialogVisible = true
      this.nodeData = nodeData
      this.$refs.deptRef.getEmployeesSimple()
      this.$refs.deptRef.getDepartDetails(nodeData.id)
    }
  }

}
</script>

<style scoped lang="scss">
.tree-card {
  padding: 30px 30px;
  font-size:14px;
}
.el-tree {
  ::v-deep {
    // 小三角的样式, 去掉默认的小三角的旋转效果
    .el-tree-node__expand-icon.expanded {
      -webkit-transform: rotate(0deg);
      transform: rotate(0deg);
    }
    // 有子节点 且未展开 +
    .el-icon-caret-right:before {
      background: url("~@/assets/common/add-circle.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 有子节点 且已展开 -
    .el-tree-node__expand-icon.expanded.el-icon-caret-right:before{
      background: url("~@/assets/common/minus-circle.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 没有子节点
    .el-tree-node__expand-icon.is-leaf::before  {
      background: url("~@/assets/common/user-filling.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
  }
}
</style>
